package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.itheima.exception.BusinessException;
import com.itheima.mapper.BrandMapper;
import com.itheima.mapper.ProductMapper;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Product;
import com.itheima.pojo.ProductQueryParam;
import com.itheima.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    // @Autowired
    // private ProductMapper productMapper;

    @Autowired
    private BrandMapper brandMapper;





    public void upOrDown(Integer id, Integer publishStatus) {
        // update product set publish_status = #{publishStatus}, update_time = now() where id = #{id}
        Product product = new Product();
        product.setId(id);
        product.setUpdateTime(LocalDateTime.now());
        product.setPublishStatus(publishStatus);
        baseMapper.updateById(product);
    }


    // @Override
    public void verify(Integer id, Integer status) {
        // Product product = new Product();
        // product.setId(id);
        // product.setVerifyStatus(status);
        // product.setUpdateTime(LocalDateTime.now());
        // productMapper.update(product);

        Product product = new Product();
        product.setId(id);
        product.setUpdateTime(LocalDateTime.now());
        product.setVerifyStatus(status);
        baseMapper.updateById(product);
    }


    @Override
    public PageBean page(ProductQueryParam queryParam) {
        Page<Product> page = new Page<>(queryParam.getPage(), queryParam.getPageSize());

        // 构建条件
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(queryParam.getName() != null, Product::getName, queryParam.getName());
        queryWrapper.eq(queryParam.getBrandId() != null, Product::getBrandId, queryParam.getBrandId());
        queryWrapper.eq(queryParam.getPublishStatus() != null, Product::getPublishStatus, queryParam.getPublishStatus());
        queryWrapper.eq(queryParam.getVerifyStatus() != null, Product::getVerifyStatus, queryParam.getVerifyStatus());

        page = this.page(page, queryWrapper);


        PageBean pageBean = new PageBean(page.getTotal(), page.getRecords());
        return pageBean;


    }

    @Override
    public List<Map> productUpOrDown() {
        return brandMapper.productUpOrDown();
    }

    @Override
    public List<Map> productStatus() {
        return brandMapper.productStatus();
    }
}
